//
//  Photo.m
//  时尚男士
//
//  Created by dlios on 15-6-17.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "Photo.h"
#import "UIImageView+AFNetworking.h"

#define Screen_Width self.view.bounds.size.width
@interface Photo ()

@property (nonatomic, assign)int flag;// 记录当前翻到了第几页
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)UIPageControl *pageControl;

@property (nonatomic, retain)NSMutableArray *photosArr;

@end

@implementation Photo

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.photosArr = [NSMutableArray arrayWithObjects:self.photoUrl,self.photoImg1,self.photoImg2, nil];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    self.view.backgroundColor = [UIColor darkGrayColor];
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 220)];
    _scroll.backgroundColor = [UIColor lightGrayColor];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width * self.photosArr.count, self.view.frame.size.height - 220);
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    [self.view addSubview:_scroll];
    
    [_scroll release];
    
    // 可以根据数组里元素的数量来 循环
    for (int index = 0; index < self.photosArr.count; index++)
    {
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * index, 0, self.view.frame.size.width, self.view.frame.size.height - 220)];
        scrollView.minimumZoomScale = 0.1;
        scrollView.maximumZoomScale = 2.f;
        scrollView.delegate = self;
        [_scroll addSubview:scrollView];
        [scrollView release];
        
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL *url = [NSURL URLWithString:[self.photosArr objectAtIndex:index]];
        [imageView setImageWithURL:url];
        imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 220);
        imageView.userInteractionEnabled = YES;
        [scrollView addSubview:imageView];
        [imageView release];
    }
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height - 40, Screen_Width - 60, 30)];
    _pageControl.numberOfPages = 4;
    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    _pageControl.pageIndicatorTintColor = [UIColor blackColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self.view addSubview:_pageControl];
    [_pageControl setDefersCurrentPageDisplay:YES];
    [_pageControl release];
}

//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page
{
    [_scroll setContentOffset:CGPointMake(_pageControl.currentPage * Screen_Width, 0) animated:YES];
}

//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scroll) {
        int halfX = scrollView.frame.size.width / 2;
        _pageControl.currentPage = (scrollView.contentOffset.x - halfX) / (scrollView.frame.size.width) + 1;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scroll == scrollView) {
        
        // 方法1: 每次减速结束都让scollView所有的scrollView子视图缩放变为1
        //        for (UIScrollView * scrollv in scrollView.subviews) {
        //            if ([scrollv isKindOfClass:[UIScrollView class]]) {
        //                scrollv.zoomScale = 1.0;
        //            }
        //        }
        
        // 方法2: 每次减速结束, 如果当前的位置(currentPage)跟之前的位置(flag)不同, 让之前的小scrollView缩放变为1, 并且把 flag 更新为 当前的位置(currentPage)
        int index = _scroll.contentOffset.x / Screen_Width;
        _pageControl.currentPage = index;
        if (index != _flag) {
            UIScrollView *tempScro = [_scroll.subviews objectAtIndex:_flag];
            tempScro.zoomScale = 1.0;
            _flag = index;
        }
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (scrollView != _scroll){
        // 只有小scrollView才允许缩放
        return [scrollView.subviews firstObject];
    }else{
        return nil;
    }
}
- (void)dealloc
{
    [_scroll release];
    [_pageControl release];
    [_photoImg1 release];
    [_photoImg2 release];
    [_photoUrl release];
    [_photosArr release];
    [_photoLabel release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
